<?php

namespace App\Http\Requests\Admin\PayAndConsume;

use App\Models\Enum\DataType;
use Illuminate\Foundation\Http\FormRequest;
use App\Services\GameTool\ChatLogService;
use Auth;

class StatServerRequest extends FormRequest
{
    public function __construct(ChatLogService $chatLogService)
    {
        $this->chatLogService = $chatLogService;
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'start_time' => 'nullable',
        ];
    }

    public function messages()
    {
        return [
        ];
    }

    protected function prepareForValidation()
    {
        //默认的 区服
        $channels = Auth::guard('admin')->user()->getChannels(['game_id' => session('game_id')], 'channel_id');
        $servers = Auth::guard('admin')->user()->getServers();
        $reqChannelId = isset($this->channel_id) ? $this->channel_id : array_keys($channels);
        if (!is_array($reqChannelId)) {
            $reqChannelId = explode(',', $reqChannelId);
        }
        $reqUniqueId = isset($this->unique_id) ? $this->unique_id : array_column($servers, 'unique_id');
        if (!is_array($reqUniqueId)) {
            $reqUniqueId = explode(',', $reqUniqueId);
        }

        $defaultChatChannels = $this->chatLogService->getChatChannelTypes();
        $defaultChatChannels = (count($defaultChatChannels) > 0 ? array_column($defaultChatChannels, 'chat_channel_id') : []);
        $reqChatChannelId = isset($this->chat_channel_id) ? $this->chat_channel_id : $defaultChatChannels;
        if (!is_array($reqChatChannelId)) {
            $reqChatChannelId = explode(',', $reqChatChannelId);
        }

        $reqStartTime = isset($this->start_time) ? $this->start_time : date('Y-m-d');
        $reqEndTime = isset($this->end_time) ? $this->end_time : date('Y-m-d');

        $reqPage = isset($this->page) ? intval($this->page) : 1;
        $reqPageSize = isset($this->page_size) ? intval($this->page_size) : 10;

        //$tradeType = isset($this->trade_type) ? strval(intval($this->trade_type)) : '0';

        //日期选择
        $reqDataType = isset($this->data_type) ? intval($this->data_type) : '0';
        if ($reqDataType > 0) {
            switch ($reqDataType) {
                case DataType::TODAY:
                    $reqStartTime = date('Y-m-d');
                    $reqEndTime = date('Y-m-d');
                    break;
                case DataType::YESTERDAY:
                    $reqStartTime = date('Y-m-d', strtotime('-1 day'));
                    $reqEndTime = date('Y-m-d', strtotime('-1 day'));
                    break;
                case DataType::SEVEN_DAYS:
                    $reqStartTime = date('Y-m-d', strtotime('-6 day'));
                    $reqEndTime = date('Y-m-d');
                    break;
                case DataType::THIRTY_DAYS:
                    $reqStartTime = date('Y-m-d', strtotime('-29 day'));
                    $reqEndTime = date('Y-m-d');
                    break;
                case DataType::HALF_YEAR:
                    $reqStartTime = date('Y-m-d', strtotime('-179 day'));
                    $reqEndTime = date('Y-m-d');
                    break;
            }
        }

        $this->merge([
            'channel_id' => $reqChannelId,
            'unique_id' => $reqUniqueId,
            'chat_channel_id' => $reqChatChannelId,

            'start_time' => $reqStartTime,
            'end_time' => $reqEndTime,

            'page' => $reqPage,
            'page_size' => $reqPageSize,
            'data_type' => $reqDataType
        ]);
    }
}
